package sys.service.impl;

import io.swagger.models.auth.In;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sys.core.ResponseResult;
import sys.dao.RoleDao;
import sys.dao.TeacherDao;
import sys.entity.PageData;
import sys.entity.TeacherEntity;
import sys.entity.vo.TeaCountVo;
import sys.entity.vo.TeacherCountVo;
import sys.service.RoleService;
import sys.service.TeacherService;
import sys.utils.ExcelUtil;
import sys.utils.PageUtil;

import javax.servlet.http.HttpServletResponse;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
@Service
public class TeacherServiceImpl implements TeacherService {

    private static final Logger log = LoggerFactory.getLogger(TeacherServiceImpl.class);


    @Autowired
    private TeacherDao dao;
    @Override
    public void add(TeacherEntity entity) {
        dao.add(entity);

    }

    @Override
    public void delete(TeacherEntity entity) {
        dao.delete(entity);

    }

    @Override
    public void update(TeacherEntity entity) {
        dao.update(entity);

    }

    @Override
    public List<TeacherEntity> select(TeacherEntity entity) {
        return dao.select(entity);
    }

    @Override
    public List<TeacherEntity> select() {
        return dao.select(null);
    }

    @Override
    public PageData<TeacherEntity> likeSelect(TeacherEntity entity) {
        return PageUtil.getPageData(entity, dao);
    }

    @Override
    public void exportExcel(TeacherEntity entity, HttpServletResponse response) {
// 获取头部信息（可以设置为动态）
        String[] headList = new String[] { "主键", "教师名", "地址", "年龄"};

        String[] headEngList = new String[]{ "id", "name", "address", "age"};

        String[] describeList = new String[] { "", "", "", ""};

        //设置头部以及描述信息
        Map<String, String> headAndDescribeMap = new LinkedHashMap<>();
        for (int i = 0; i < headEngList.length; i++) {
            headAndDescribeMap.put(headEngList[i], describeList[i]);
        }
        String name = "积云";

        ExcelUtil.exportExcel(entity, response, dao, headList, headAndDescribeMap,name);
    }

    @Override
    public ResponseResult teacherCount() {

        //获取学生最多的老师
        String maxStudentTeacher = dao.getMaxStudentTeacher();
        log.info("学生最多的老师:{}",maxStudentTeacher);
        //获取学生数大于某个值的老师的名字
        Integer num = 0;
        List<String> studentTeacher = dao.getStudentTeacher(num);
        log.info("学生数大于:{}的老师的名字:{}",num,studentTeacher);
        //组装数据
        TeacherCountVo teacherCountVo = new TeacherCountVo();
        teacherCountVo.setName(maxStudentTeacher);
        teacherCountVo.setNameList(studentTeacher);
        return ResponseResult.success(teacherCountVo);
    }

    /**
     * 教师统计
     * @return
     */
    @Override
    public ResponseResult count() {
        //获取学生最多的老师的名字
        String name = dao.getMaxStuTeacher();
        log.info("学生最多的老师的名字:{}",name);
        List<String> maxStudentNum = dao.getMaxStudentNum(0);
        TeaCountVo teaCountVo = new TeaCountVo();
        teaCountVo.setName(name);
        teaCountVo.setNameList(maxStudentNum);
        log.info("teaCountVo:{}",teaCountVo);
        return ResponseResult.success(teaCountVo);
    }
}
